package inc;

import java.util.List;

import org.hibernate.Session;


/*
 * 分类算法类，通过Hibernate实现
 */
public class Page<E> {

	private Session s;  //org.hibernate.Session
	private String className;  //要查询的类名
	private int size; // 总页数
	private int rows;  //总记录数
	private int pageSize; // 每页大小
	private int pageNo; // 当前页
	private String hql; // 查询语句

	public long size() {
		return size / pageSize + 1;
	}
	
	public int getRows() {
		return rows;
	}

	public List getPage(int pageNo) {
		this.pageNo = pageNo - 1;
		
		
		s.beginTransaction();
		List list = s.createQuery(hql).setFirstResult(pageNo * pageSize)
				.setMaxResults(pageSize).list();
		s.getTransaction().commit();
		return list;
	}
	
	//构造方法
	public Page(Class c, String sqlCondition, Session s, int pageSize) {
		this.s = s;
		this.className = c.getName();
		if (className.contains(".")) {
			className = className.substring(className.lastIndexOf(".") + 1,
					className.length());
		}

		s.beginTransaction();
		hql = "from " + className;
		if(sqlCondition != null) {
			hql += sqlCondition;
		}
		
		this.size = s.createQuery(hql).list().size();
		rows =  size;
		s.getTransaction().commit();

		this.pageSize = pageSize;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
